package com.parking.mgr.useraccount.controller;

import com.parking.mgr.common.annotion.BusinessLog;
import com.parking.mgr.common.pojo.response.Result;
import com.parking.mgr.useraccount.model.param.SysLoginParam;
import com.parking.mgr.useraccount.service.AuthCodeService;
import com.parking.mgr.useraccount.service.AuthService;
import com.parking.mgr.useraccount.service.LoginTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.parking.mgr.common.enums.LogAnnotionOpTypeEnum.LOGIN;
import static com.parking.mgr.common.enums.LogAnnotionOpTypeEnum.LOGOUT;

/**
 * @ClassName UserLoginController
 * @Description 登录控制器
 * @Auther: zhijianpu
 * @Date: 2023/5/23
 */
@Log4j2
@Api(value = "用户登录", tags = {"用户登录"})
@RequestMapping("/api/user/login")
@RestController
public class UserLoginController {

    @Resource
    private AuthService authService;

    @Resource
    private LoginTokenService loginTokenService;

    @Resource
    private AuthCodeService authCodeService;

//    /**
//     * 生成验证码
//     */
//    @GetMapping(value = "/verifycodeimage", produces = MediaType.IMAGE_JPEG_VALUE)
//    @ApiOperation(value = "生成验证码", notes = "生成验证码", httpMethod = "GET")
//    @BusinessLog(title = "生成验证码")
//    public void identifyImage(HttpServletRequest request, HttpServletResponse response,
//                              @ApiParam(name = "eventCode", value = "事件编码", required = true)
//                              @RequestParam(name = "eventCode") String eventCode,
//                              @ApiParam(name = "clientId", value = "客户端ID", required = true)
//                              @RequestParam(name = "clientId") String clientId
//    ) throws IOException {
//        authCodeService.identifyImageCode(request, response, eventCode, clientId);
//    }


    @PostMapping("/createclientid")
    @ApiOperation(value = "生成ClientId", notes = "生成ClientId", httpMethod = "POST")
    public Result<?> createClientId(HttpServletRequest request) {

        String clientId = authService.createClientId(request);

        Map<String, Object> clientIdMap = new HashMap<>();
        clientIdMap.put("clientId", clientId);

        return Result.success(clientIdMap);
    }

//    @GetMapping("/logout")
//    @BusinessLog(title = "退出登录", opType = LOGOUT)
//    @ApiOperation(value = "退出登录", notes = "退出登录", httpMethod = "GET")
//    public Result<?> logout(HttpServletRequest request) {
//        String clientId = authService.logout(request);
//        Map<String, Object> resultMap = new HashMap<>();
//        resultMap.put("clientId", clientId);
//        return Result.success(resultMap);
//    }


    /**
     * 账号密码登录
     *
     * @param sysLoginParam
     * @return
     */
    @PostMapping("/dologin")
    @BusinessLog(title = "账号密码登录", opType = LOGIN)
    @ApiOperation(value = "账号密码登录", notes = "账号密码登录", httpMethod = "POST")
    public Result<?> login(@RequestBody @Validated SysLoginParam sysLoginParam, HttpServletRequest request) {

        String token = authService.login(sysLoginParam, request);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("loginToken", token);

        return Result.success(resultMap);
    }


    /**
     * 登录令牌获取用户账号
     *
     * @param request
     * @return
     */
    @GetMapping("/getpassportid")
    @BusinessLog(title = "登录令牌获取用户账号", opType = LOGIN)
    @ApiOperation(value = "登录令牌获取用户账号", notes = "登录令牌获取用户账号", httpMethod = "GET")
    public Result<?> getPassportIdByToken(HttpServletRequest request) {
        return Result.success(loginTokenService.getPassportIdByToken(request));
    }

    @GetMapping("/logout")
    @BusinessLog(title = "退出登录", opType = LOGOUT)
    @ApiOperation(value = "退出登录", notes = "退出登录", httpMethod = "GET")
    public Result<?> logout(HttpServletRequest request) {
        String clientId = authService.logout(request);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("clientId", clientId);
        return Result.success(resultMap);
    }

}
